package com.jushu.storbox.database;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FolderInfo;
import com.jushu.storbox.service.FileHelper;
import com.jushu.storbox.util.ChinsesCharComp;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.model.response.FolderResponse;

public class TeamSpaceFolderDao  extends BaseDao implements ITeamSpaceFolderDao {
	public String TABLE_NAME;
	public final static String LOG_TAG = "TeamSpaceFolderDao";
	private String mTeamSpaceId;

	/**
	 * 
	 */
	public TeamSpaceFolderDao() {
	}
	
	/**
	 * 
	 */
	public TeamSpaceFolderDao(String tablename) {
		TABLE_NAME = tablename;
	}
	
	

	public String getTeamSpaceId() {
		return mTeamSpaceId;
	}

	public void setTeamSpaceId(String mTeamSpaceId) {
		this.mTeamSpaceId = mTeamSpaceId;
	}

	@Override
	public void insertFolder(FolderResponse folderResponse, int syncStu) {
		FolderInfo folderInfo = getFolderById(folderResponse.getId());
		if(null != folderInfo){
			LogUtil.e(LOG_TAG, "database FolderDao folderResponse.getOwnerBy():"+folderInfo.getOwnerBy());
		}
		if(null != folderInfo && null != folderInfo.getId() && folderInfo.getId().trim().equals(folderResponse.getId().trim())){
			LogUtil.e(LOG_TAG , "FolderDao no need insertFile, id identical");
		}else{
			try {
				ContentValues cv = new ContentValues();
				cv.put(IBaseDao.id, folderResponse.getId());
				cv.put(IBaseDao.file_name, folderResponse.getName());
				cv.put(IBaseDao.is_file, 0);
				cv.put(IBaseDao.owner_by, folderResponse.getOwnerBy());
				cv.put(IBaseDao.parent_folder_id, folderResponse.getParent());
				cv.put(IBaseDao.file_size, folderResponse.getSize());
				cv.put(IBaseDao.server_ctime, folderResponse.getCreatedAt());
				cv.put(IBaseDao.server_mtime, folderResponse.getModifiedAt());
				cv.put(IBaseDao.last_refresh_time, 0);
				cv.put(IBaseDao.local_path, "");
				cv.put(IBaseDao.client_ctime, folderResponse.getContentCreatedAt());
				cv.put(IBaseDao.client_mtime, folderResponse.getContentModifiedAt());
				cv.put(IBaseDao.static_status, folderResponse.getStatus());
				cv.put(IBaseDao.trans_status, 0);
				cv.put(IBaseDao.trans_status_ref_cnt, 0);
				cv.put(IBaseDao.is_exist, 0);
				cv.put(IBaseDao.local_operation_time, Calendar.getInstance()
						.getTimeInMillis());
				cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance()
						.getTimeInMillis());
				cv.put(IBaseDao.description, folderResponse.getDescription());
				cv.put(IBaseDao.isencrypt, folderResponse.getIsEncrypt());
				cv.put(IBaseDao.isshare, folderResponse.getIsShare());
				cv.put(IBaseDao.isshare_link, folderResponse.getIsSharelink());
				cv.put(IBaseDao.isSync, syncStu);
				cv.put(IBaseDao.teamspace_id, mTeamSpaceId);
				this.getDb().insert(TABLE_NAME, null, cv);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				LogUtil.e(LOG_TAG,
						"insertFolder is fail:" + e.getLocalizedMessage());
			}
		}
	}

	@Override
	public FileFolderInfo insertFolders(FolderResponse folderResponse,
			int syncStu) throws ClientException {
		ContentValues cv = new ContentValues();
		cv.put(IBaseDao.id, folderResponse.getId());
		cv.put(IBaseDao.file_name, folderResponse.getName());
		cv.put(IBaseDao.is_file, 0);
		cv.put(IBaseDao.owner_by, folderResponse.getOwnerBy());
		cv.put(IBaseDao.parent_folder_id, folderResponse.getParent());
		cv.put(IBaseDao.file_size, folderResponse.getSize());
		cv.put(IBaseDao.server_ctime, folderResponse.getCreatedAt());
		cv.put(IBaseDao.server_mtime, folderResponse.getModifiedAt());
		cv.put(IBaseDao.last_refresh_time, 0);
		cv.put(IBaseDao.local_path, "");
		cv.put(IBaseDao.client_ctime, folderResponse.getContentCreatedAt());
		cv.put(IBaseDao.client_mtime, folderResponse.getContentModifiedAt());
		cv.put(IBaseDao.static_status, folderResponse.getStatus());
		cv.put(IBaseDao.trans_status, 0);
		cv.put(IBaseDao.trans_status_ref_cnt, 0);
		cv.put(IBaseDao.is_exist, 0);
		cv.put(IBaseDao.local_operation_time, Calendar.getInstance()
				.getTimeInMillis());
		cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance()
				.getTimeInMillis());
		cv.put(IBaseDao.description, folderResponse.getDescription());
		cv.put(IBaseDao.isencrypt, folderResponse.getIsEncrypt());
		cv.put(IBaseDao.isshare, folderResponse.getIsShare());
		cv.put(IBaseDao.isshare_link, folderResponse.getIsSharelink());
		cv.put(IBaseDao.isSync, syncStu);
		cv.put(IBaseDao.teamspace_id, mTeamSpaceId);
		this.getDb().insert(TABLE_NAME, null, cv);
//		FileHelper.createFolder(folderResponse);
		return FileHelper.createFolder(folderResponse);
	}

	@Override
	public void updateFolder(FolderResponse folderInfoResponse) {
		try {
			String where = id + "=?" + " and " + IBaseDao.teamspace_id + " = ?";
			String[] whereValue = { folderInfoResponse.getId(), mTeamSpaceId };
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.id, folderInfoResponse.getId());
			cv.put(IBaseDao.file_name, folderInfoResponse.getName());
			cv.put(IBaseDao.is_file, 0);
			cv.put(IBaseDao.owner_by, folderInfoResponse.getOwnerBy());
			cv.put(IBaseDao.parent_folder_id, folderInfoResponse.getParent());
			cv.put(IBaseDao.file_size, folderInfoResponse.getSize());
			cv.put(IBaseDao.server_ctime, folderInfoResponse.getCreatedAt());
			cv.put(IBaseDao.server_mtime, folderInfoResponse.getModifiedAt());
			cv.put(IBaseDao.local_path, "");
			cv.put(IBaseDao.client_ctime,
					folderInfoResponse.getContentCreatedAt());
			cv.put(IBaseDao.client_mtime,
					folderInfoResponse.getContentModifiedAt());
			cv.put(IBaseDao.last_refresh_time, 0);
			cv.put(IBaseDao.static_status, folderInfoResponse.getStatus());
			// cv.put(IBaseDao.trans_status, 0);
			cv.put(IBaseDao.trans_status_ref_cnt, 0);
			cv.put(IBaseDao.is_exist, 0);
			cv.put(IBaseDao.local_operation_time, Calendar.getInstance()
					.getTimeInMillis());
			cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance()
					.getTimeInMillis());
			cv.put(IBaseDao.description, folderInfoResponse.getDescription());
			cv.put(IBaseDao.isencrypt, folderInfoResponse.getIsEncrypt());
			cv.put(IBaseDao.isshare, folderInfoResponse.getIsShare());
			// cv.put(IBaseDao.isSync, folderInfoResponse.getIsSync() ? 1 : 0);
			this.getDb().update(TABLE_NAME, cv, where, whereValue);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateFolder is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public void updateFolder(FolderInfo folderInfo) {// 报错
		try {
			String where = id + "=?" + " and " + IBaseDao.teamspace_id + " = ?";
			String[] whereValue = { folderInfo.getId(), mTeamSpaceId};
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.id, folderInfo.getId());
			cv.put(IBaseDao.file_name, folderInfo.getName());
			cv.put(IBaseDao.is_file, 0);
			cv.put(IBaseDao.owner_by, folderInfo.getOwnerBy());
			cv.put(IBaseDao.parent_folder_id, folderInfo.getParent());
			cv.put(IBaseDao.file_size, folderInfo.getSize());
			cv.put(IBaseDao.server_ctime, folderInfo.getCreatedAt());
			cv.put(IBaseDao.server_mtime, folderInfo.getModifiedAt());
			cv.put(IBaseDao.local_path, "");
			cv.put(IBaseDao.client_ctime, folderInfo.getContentCreatedAt());
			cv.put(IBaseDao.client_mtime, folderInfo.getContentModifiedAt());
			cv.put(IBaseDao.last_refresh_time, folderInfo.getLastRefreshTime());
			if (null != folderInfo.getStatus())
				cv.put(IBaseDao.static_status, folderInfo.getStatus());
			else
				cv.put(IBaseDao.static_status, "");
			cv.put(IBaseDao.trans_status, 0);
			cv.put(IBaseDao.trans_status_ref_cnt, 0);
			cv.put(IBaseDao.is_exist, 0);
			cv.put(IBaseDao.local_operation_time, Calendar.getInstance()
					.getTimeInMillis());
			cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance()
					.getTimeInMillis());
			if (null != folderInfo.getDescription())
				cv.put(IBaseDao.description, folderInfo.getDescription());
			else
				cv.put(IBaseDao.description, "");
			cv.put(IBaseDao.isencrypt, folderInfo.isEncrypt());
			cv.put(IBaseDao.isshare, folderInfo.isShare());
			cv.put(IBaseDao.isSync, folderInfo.isSync() ? 1 : 0);
			int ret = this.getDb().update(TABLE_NAME, cv, where, whereValue);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateFolder is fail:" + e.getLocalizedMessage());
		}
	}
	@Override
	public void insertOrUpdateFolder(FolderInfo folderInfo, Context context) {
		// TODO Auto-generated method stub
		try {

			String where = id + "=?" + " and " + IBaseDao.teamspace_id + " = ?";
			String[] whereValue = { folderInfo.getId() , mTeamSpaceId};
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.id, folderInfo.getId());
			cv.put(IBaseDao.file_name, folderInfo.getName());
			cv.put(IBaseDao.is_file, 0);
			cv.put(IBaseDao.owner_by, folderInfo.getOwnerBy());
			cv.put(IBaseDao.parent_folder_id, folderInfo.getParent());
			cv.put(IBaseDao.file_size, folderInfo.getSize());
			cv.put(IBaseDao.server_ctime, folderInfo.getCreatedAt());
			cv.put(IBaseDao.server_mtime, folderInfo.getModifiedAt());
			cv.put(IBaseDao.local_path, "");
			cv.put(IBaseDao.client_ctime, folderInfo.getContentCreatedAt());
			cv.put(IBaseDao.client_mtime, folderInfo.getContentModifiedAt());
			cv.put(IBaseDao.last_refresh_time, folderInfo.getLastRefreshTime());
			if (null != folderInfo.getStatus())
				cv.put(IBaseDao.static_status, folderInfo.getStatus());
			else
				cv.put(IBaseDao.static_status, "");
			cv.put(IBaseDao.trans_status, 0);
			cv.put(IBaseDao.trans_status_ref_cnt, 0);
			cv.put(IBaseDao.is_exist, 0);
			cv.put(IBaseDao.local_operation_time, Calendar.getInstance()
					.getTimeInMillis());
			cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance()
					.getTimeInMillis());
			if (null != folderInfo.getDescription())
				cv.put(IBaseDao.description, folderInfo.getDescription());
			else
				cv.put(IBaseDao.description, "");
			cv.put(IBaseDao.isencrypt, folderInfo.isEncrypt());
			cv.put(IBaseDao.isshare, folderInfo.isShare());
			cv.put(IBaseDao.isSync, folderInfo.isSync() ? 1 : 0);
			cv.put(IBaseDao.teamspace_id, mTeamSpaceId);
			FolderInfo info = getFolderById(folderInfo.getId());
			if (null != info) {
				Log.i("liuxiangliu", info.getName() + ":info name");
				int ret = this.getDb()
						.update(TABLE_NAME, cv, where, whereValue);
			} else {
				Log.i("liuxiangliu",  "info null");
				this.getDb().insert(TABLE_NAME, null, cv);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"insertFolder is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public List<FolderInfo> getFolderList(String folderId) {
		List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
		try {
			if (null != folderId) {
				Cursor cursor = this.getDb().rawQuery(
						"select * from " + TABLE_NAME
								+ " where parent_folder_id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { folderId , mTeamSpaceId});
				// 判断游标是否为空
				if (false == cursor.moveToFirst()) {
					return fileInfoList;
				}
				do {
					FolderInfo folderInfo = new FolderInfo();
					folderInfo.fromCursor(cursor);
					fileInfoList.add(folderInfo);
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getFolderList is fail:" + e.getLocalizedMessage());
		}
		return fileInfoList;
	}

	// flag 0:my cloud page, 1: shared page
	@Override
	public List<FolderInfo> getFolderListPage(int flag, String folderId) {
		if (null != folderId) {
			int number = ClientConfig.CLOUD_COUNT_PER_PAGE;// 是每次要加载的个数
			int count = ShareDriveApplication.getInstance()
					.getMyCloudDrivePage() * number;// page是加载的页数。
			if (Constant.SHARED_GET_FILE_LIST_STATE == flag) {
				number = ClientConfig.SHARED_COUNT_PER_PAGE;//
				count = ShareDriveApplication.getInstance().getMySharePage()
						* number;// page是加载的页数。
			}

			List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
			try {
				if (null != folderId) {

					Cursor cursor = this
							.getDb()
							.rawQuery(
									"select * from "
											+ TABLE_NAME
											+ " where parent_folder_id = ?"  + " and " + IBaseDao.teamspace_id + " = ?" + " Order by server_mtime desc"
											+ " limit ?" + " offset ?",
									new String[] { folderId, mTeamSpaceId,
											String.valueOf(number),
											String.valueOf(count) });
					// 判断游标是否为空
					if (false == cursor.moveToFirst()) {
						return fileInfoList;
					}
					do {
						FolderInfo folderInfo = new FolderInfo();
						folderInfo.fromCursor(cursor);
						fileInfoList.add(folderInfo);
					} while (cursor.moveToNext());
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				LogUtil.e(LOG_TAG,
						"getFolderListPage is fail:" + e.getLocalizedMessage());
			}
			return fileInfoList;
		} else {
			LogUtil.e(LOG_TAG, "folderId is null");
		}
		return null;
	}
	
	

	@Override
	public List<FolderInfo> getFolderListPage(int flag, String folderId,
			String des, String orderBy) {
		String dbDes = "DESC";
		String dbOrderBy = "server_mtime";
		if ("DESC".equals(des)) {
			dbDes = "DESC";
		} else {// 升序
			dbDes = "ASC";
		}
		if ("size".equals(orderBy)) {
			dbOrderBy = file_size;
		} else if ("name".equals(orderBy)) {
			return getFolderListPageOrderByName(flag, folderId, des, dbOrderBy);
		} else {
			dbOrderBy = server_mtime;
		}
		if (null != folderId) {
			int number = ClientConfig.CLOUD_COUNT_PER_PAGE;// 是每次要加载的个数
			int count = ShareDriveApplication.getInstance()
					.getMyCloudDrivePage() * number;// page是加载的页数。
			if (Constant.SHARED_GET_FILE_LIST_STATE == flag) {
				number = ClientConfig.SHARED_COUNT_PER_PAGE;//
				count = ShareDriveApplication.getInstance().getMySharePage()
						* number;// page是加载的页数。
			}

			List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
			Log.i("liuxiangliu", mTeamSpaceId + ":mteamspaceid");
			Log.i("liuxiangliu", folderId + ":folderid");
			try {
				if (null != folderId) {
					Cursor cursor = this.getDb().rawQuery(
							"select * from " + TABLE_NAME
									+ " where parent_folder_id = ?" + " and " + IBaseDao.teamspace_id + " = ?"  + " Order by "
									+ dbOrderBy + " " + dbDes + " limit ?"
									+ " offset ?",
							new String[] { folderId, mTeamSpaceId, String.valueOf(number), 
									String.valueOf(count) });
					// 判断游标是否为空
					if (false == cursor.moveToFirst()) {
						Log.i("liuxiangliu", "getFolderListPage size 0");
						return fileInfoList;
					}
					do {
						FolderInfo folderInfo = new FolderInfo();
						folderInfo.fromCursor(cursor);
						fileInfoList.add(folderInfo);
					} while (cursor.moveToNext());
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				LogUtil.e(LOG_TAG,
						"getFolderListPage is fail:" + e.getLocalizedMessage());
			}
			return fileInfoList;
		} else {
			LogUtil.e(LOG_TAG, "folderId is null");
		}
		return null;
	}
	
	public List<FolderInfo> getFolderListPageOrderByName(int flag, String folderId,
			String des, String orderBy){
		String dbDes = "desc";
		String dbOrderBy = "server_mtime";
		if ("DESC".equals(des)) {
			dbDes = "DESC";
		} else {// 升序
			dbDes = "ASC";
		}
//		if ("size".equals(orderBy)) {
//			dbOrderBy = file_size;
//		} else if ("name".equals(orderBy)) {
//			dbOrderBy = file_name;
//		} else {
//			dbOrderBy = server_mtime;
//		}
		if (null != folderId) {
			int number = ClientConfig.CLOUD_COUNT_PER_PAGE;// 是每次要加载的个数
			int count = ShareDriveApplication.getInstance()
					.getMyCloudDrivePage() * number;// page是加载的页数。
			if (Constant.SHARED_GET_FILE_LIST_STATE == flag) {
				number = ClientConfig.SHARED_COUNT_PER_PAGE;//
				count = ShareDriveApplication.getInstance().getMySharePage()
						* number;// page是加载的页数。
			}

			List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
			try {
				if (null != folderId) {

					Cursor cursor = this.getDb().rawQuery(
							"select * from " + TABLE_NAME
									+ " where parent_folder_id = ?"  + " and " + IBaseDao.teamspace_id + " = ?" + " Order by "
									+ dbOrderBy + " " + dbDes + " limit ?"
									+ " offset ?",
							new String[] { folderId, mTeamSpaceId,
									String.valueOf(number),
									String.valueOf(count) });
					// 判断游标是否为空
					if (false == cursor.moveToFirst()) {
						return fileInfoList;
					}
					do {
						FolderInfo folderInfo = new FolderInfo();
						folderInfo.fromCursor(cursor);
						fileInfoList.add(folderInfo);
					} while (cursor.moveToNext());
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				LogUtil.e(LOG_TAG,
						"getFolderListPage is fail:" + e.getLocalizedMessage());
			}
			
			Iterator<FolderInfo> iterator=fileInfoList.iterator();
			List<FolderInfo> fileInfoListEn = new ArrayList<FolderInfo>();
			List<FolderInfo> fileInfoListCn = new ArrayList<FolderInfo>();
			while(iterator.hasNext()){
				FolderInfo info=iterator.next();
				char[] charOfCN = info.getName().toCharArray();
				if(charOfCN[0]>128){
					fileInfoListCn.add(info);
				}else {
					fileInfoListEn.add(info);
				}
			}
			
			ChinsesCharComp cmp = new ChinsesCharComp();
			cmp.setOrder(dbDes);
			Collections.sort(fileInfoListEn, cmp);
			cmp.setOrder(dbDes);
			Collections.sort(fileInfoListCn, cmp);
			fileInfoList=new ArrayList<FolderInfo>();
			if ("DESC".equals(des)) {
				fileInfoList.addAll(fileInfoListEn);
				fileInfoList.addAll(fileInfoListCn);
			}else{
				fileInfoList.addAll(fileInfoListCn);
				fileInfoList.addAll(fileInfoListEn);
			}
			return fileInfoList;
		} else {
			LogUtil.e(LOG_TAG, "folderId is null");
		}
		return null;
	}

	@Override
	public void deleteFolder(String id) {
		try {
			if (null != id) {
				this.getDb().execSQL(
						"delete from " + TABLE_NAME + " where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { id , mTeamSpaceId});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"deleteFolder is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public FolderInfo getFolderById(String folderId) {
		FolderInfo folderInfo = null;
		try {
			if (null != folderId) {
				Cursor cursor = this.getDb().query(
						TABLE_NAME,
						new String[] { FIELD_ID, id, file_name, owner_by,
								is_file, parent_folder_id, file_size,
								server_ctime, server_mtime, last_refresh_time,
								local_path, client_ctime, client_mtime,
								static_status, trans_status,
								trans_status_ref_cnt, is_exist,
								local_operation_time, local_last_modify_time,
								description, isencrypt, isshare, isshare_link,
								isSync }, id + " = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { folderId ,mTeamSpaceId}, null, null, " id desc");
				if (cursor.moveToFirst()) {
					folderInfo = new FolderInfo();
					folderInfo.fromCursor(cursor);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getFolderById is fail:" + e.getLocalizedMessage());
		}
		return folderInfo;
	}

	@Override
	public FileFolderInfo getFileFolderById(String folderId) {
		FileFolderInfo folderInfo = null;
		try {
			if (null != folderId) {
				Cursor cursor = this.getDb().query(
						TABLE_NAME,
						new String[] { FIELD_ID, id, file_name, owner_by,
								is_file, parent_folder_id, file_size,
								server_ctime, server_mtime, last_refresh_time,
								local_path, client_ctime, client_mtime,
								static_status, trans_status,
								trans_status_ref_cnt, is_exist,
								local_operation_time, local_last_modify_time,
								description, isencrypt, isshare, isshare_link,
								isSync }, id + " = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { folderId , mTeamSpaceId}, null, null, " id desc");
				if (cursor.moveToFirst()) {
					folderInfo = new FolderInfo();
					folderInfo.fromCursor(cursor);
				}
			}
		} catch (Exception e) {
			// TODO: handle exceptio
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getFileFolderById is fail:" + e.getLocalizedMessage());
		}
		return folderInfo;
	}

	@Override
	public void updateLocalPath(String folderId, String localPath) {
		try {
			if (null != folderId) {
				this.getDb().execSQL(
						"update  " + TABLE_NAME + " set " + IBaseDao.local_path
								+ "=? where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { localPath, folderId, mTeamSpaceId});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateLocalPath is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public void updateTransStatus(int status, String folderId) {
		try {
			String where = id + "=?" + " and " + IBaseDao.teamspace_id + " = ?";
			String[] whereValue = { folderId , mTeamSpaceId};
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.trans_status, status);
			this.getDb().update(TABLE_NAME, cv, where, whereValue);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateTransStatus is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public int updateSyncStatuc(int syncStatus, String folderId) {
		try {
			String where = id + " = ?" + " and " + IBaseDao.teamspace_id + " = ?";
			String[] whereValue = { folderId , mTeamSpaceId};
			ContentValues cv = new ContentValues();
			cv.put(isSync, syncStatus);
			return this.getDb().update(TABLE_NAME, cv, where, whereValue);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateSyncStatuc is fail:" + e.getLocalizedMessage());
		}
		return -1;
	}

	@Override
	public void updateLastRefreshTime(String folderId, long lastRefreshTime) {
		try {
			if (null != folderId) {
				this.getDb().execSQL(
						"update  " + TABLE_NAME + " set "
								+ IBaseDao.last_refresh_time
								+ " = ? where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { "" + lastRefreshTime, folderId, mTeamSpaceId});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateLastRefreshTime is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public long getLastRefreshTime(String folderId) {
		long lastRefreshTime = 0;
		try {
			if (null != folderId) {
				Cursor cursor = this.getDb().rawQuery(
						"select * from  " + TABLE_NAME + " where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { folderId , mTeamSpaceId});
				if (cursor.moveToFirst()) {
					lastRefreshTime = cursor.getLong(cursor
							.getColumnIndex(IBaseDao.last_refresh_time));
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getLastRefreshTime is fail:" + e.getLocalizedMessage());
		}
		return lastRefreshTime;
	}

	public long getLastUpdateTime(String folderId) {
		long lastUpdateTime = 0;
		try {
			if (null != folderId) {
				Cursor cursor = this.getDb().rawQuery(
						"select * from  " + TABLE_NAME + " where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { folderId, mTeamSpaceId });
				if (cursor.moveToFirst()) {
					lastUpdateTime = cursor.getLong(cursor
							.getColumnIndex(IBaseDao.local_last_modify_time));
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getLastUpdateTime is fail:" + e.getLocalizedMessage());
		}
		return lastUpdateTime;
	}

	public void updateLastUpdateTime(String folderId) {
		try {
			if (null != folderId) {
				this.getDb().execSQL(
						"update  " + TABLE_NAME + " set "
								+ IBaseDao.local_last_modify_time
								+ "=? where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] {
								"" + Calendar.getInstance().getTimeInMillis(),
								folderId , mTeamSpaceId});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateLastUpdateTime is fail:" + e.getLocalizedMessage());
		}

	}

	/**
	 * 
	 * 重命名（文件夹）
	 * 
	 * @param context
	 * @param name
	 */
	public void renameFolder(String name, FolderResponse folderResponse,
			String folderID) {
		try {
			if (null != folderID) {
				this.getDb()
						.execSQL(
								"update " + TABLE_NAME + " set "
										+ IBaseDao.file_name + " = ? , "
										+ IBaseDao.server_mtime
										+ " = ? where id = ?"  + " and " + IBaseDao.teamspace_id + " = ?",
								new String[] { name,
										folderResponse.getModifiedAt() + "",
										folderID , mTeamSpaceId});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"renameFolder is fail:" + e.getLocalizedMessage());
		}
	}

	/**
	 * 移动（文件夹）
	 * 
	 * @param context
	 * @param parentID
	 * @param FolderID
	 */
	public void moveFolder(String parentID, String folderID) {
		try {
			if (null != folderID) {
				this.getDb().execSQL(
						"update " + TABLE_NAME + " set "
								+ IBaseDao.parent_folder_id
								+ " = ? where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { parentID, folderID , mTeamSpaceId});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG, "moveFolder is fail:" + e.getLocalizedMessage());
		}
	}

	/**
	 * 清楚数据库
	 */
	public void deleteAll() {
		// TODO Auto-generated method stub
		try {
			this.getDb().delete(TABLE_NAME, null, null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG, "deleteAll is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public FileFolderInfo getFolderByName(String folderName) {
		// TODO Auto-generated method stub
		FileFolderInfo folderInfo = null;
		try {
			if (null != folderName) {
				Cursor cursor = this.getDb().query(
						TABLE_NAME,
						new String[] { FIELD_ID, id, file_name, owner_by,
								is_file, parent_folder_id, file_size,
								server_ctime, server_mtime, last_refresh_time,
								local_path, client_ctime, client_mtime,
								static_status, trans_status,
								trans_status_ref_cnt, is_exist,
								local_operation_time, local_last_modify_time,
								description, isencrypt, isshare, isshare_link,
								isSync }, file_name + " = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { folderName , mTeamSpaceId}, null, null, " id desc");
				if (cursor.moveToFirst()) {
					folderInfo = new FolderInfo();
					folderInfo.fromCursor(cursor);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getFolderByName is fail:" + e.getLocalizedMessage());
		}
		return folderInfo;
	}

	@Override
	public List<FileFolderInfo> getSearchFolderList(String fileName) {

		List<FileFolderInfo> fileInfoList = new ArrayList<FileFolderInfo>();
		try {
			Cursor cursor = this.getDb().rawQuery(
					"select * from " + TABLE_NAME + " where "
							+ IBaseDao.file_name + " like ?",
					new String[] { "%" + fileName + "%" });
			// 判断游标是否为空
			if (!cursor.moveToFirst()) {
				return fileInfoList;
			}
			do {
				FolderInfo folderInfo = new FolderInfo();
				folderInfo.fromCursor(cursor);
				fileInfoList.add(folderInfo);
			} while (cursor.moveToNext());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getFolderList is fail:" + e.getLocalizedMessage());
		}
		return fileInfoList;
	}

	@Override
	public void setChangedFolderId(String folderId, String changeFolderId) {
		// TODO Auto-generated method stub
		try {
			if (null != folderId) {
				this.getDb()
						.execSQL(
								"update  " + TABLE_NAME + " set "
										+ IBaseDao.changed_folderId
										+ "=? where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
								new String[] { "" + changeFolderId, folderId, mTeamSpaceId });
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateLastRefreshTime is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public String getChangedFolderId(String folderId) {
		// TODO Auto-generated method stub
		String changedFolderId = "0";
		try {
			if (null != folderId) {
				Cursor cursor = this.getDb().rawQuery(
						"select * from  " + TABLE_NAME + " where id = ?" + " and " + IBaseDao.teamspace_id + " = ?",
						new String[] { folderId , mTeamSpaceId});
				if (cursor.moveToFirst()) {
					changedFolderId = cursor.getString(cursor
							.getColumnIndex(IBaseDao.changed_folderId));
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getLastRefreshTime is fail:" + e.getLocalizedMessage());
		}
		return changedFolderId;
	}
}
